﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kontrolki;
using KPWeb.Class;
using KPWeb.Data;
using KPWeb.Function;
using KPWeb.Reports;
using MSExcel = Microsoft.Office.Interop.Excel;
//using Microsoft.Reporting.WebForms;


namespace KPWeb.KartaPracy
{

    public partial class KartaPracyEdycja : KPPage
    {
        private readonly string[] MonthNames = { "Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień" };
        private readonly string[] KwartalNames = { "I", "II", "III", "IV" };
        private DateTime dataOd //Okres wybranej Karty Pracy
        {
            get
            {
                return DateTime.Parse(ViewState["dataOd"].ToString());
            }
            set
            {
                ViewState["dataOd"] = value;
            }
        }
        private DateTime dataDo
        {
            get
            {
                return DateTime.Parse(ViewState["dataDo"].ToString());
            }
            set
            {
                ViewState["dataDo"] = value;
            }
        }
        private void komunikatError(int kod)//, int iT, int iID)
        {
            fError.Visible = true;
            switch (kod)
            {
                case -1: Error1.Visible = true;
                    break;
                case -2: Error2.Visible = true;
                    break;
                case -3: Error3.Visible = true;
                    break;
                case -4: Error4.Visible = true;
                    break;
                case -5: Error5.Visible = true;
                    break;
                default: fError.Visible = false;
                    break;
            }
        }

        private void inicKartaPracy(int iValue)
        {

            int aktYear = int.Parse(dYear.SelectedValue);
            //int iValue=int.Parse(Request["value"]);
            lRok.Text = aktYear.ToString();
            if (iValue <= 12 && iValue >= 1)
            {
                dataOd = new DateTime(aktYear, iValue, 1);
                dataDo = new DateTime(aktYear, iValue, DateTime.DaysInMonth(aktYear, iValue), 23, 59, 59);
            }
            else
                if (iValue == 101 || iValue == 104 || iValue == 107 || iValue == 110)
                {
                    dataOd = new DateTime(aktYear, iValue - 100, 1);
                    dataDo = new DateTime(aktYear, iValue - 100 + 2, DateTime.DaysInMonth(aktYear, iValue - 100 + 2), 23, 59, 59);
                }
                else
                    if (iValue == 201 || iValue == 202 || iValue == 203)
                    {
                        switch (iValue)
                        {
                            case 201:
                                dataOd = new DateTime(aktYear, 1, 1);
                                dataDo = new DateTime(aktYear, 6, DateTime.DaysInMonth(aktYear, 6), 23, 59, 59);
                                break;
                            case 202:
                                dataOd = new DateTime(aktYear, 7, 1);
                                dataDo = new DateTime(aktYear, 12, DateTime.DaysInMonth(aktYear, 12), 23, 59, 59);
                                break;
                            case 203:
                                dataOd = new DateTime(aktYear, 1, 1);
                                dataDo = new DateTime(aktYear, 12, DateTime.DaysInMonth(aktYear, 12), 23, 59, 59);
                                break;
                        }
                    }
                    else
                    {
                        komunikatError(-4);
                        return;
                    }

            hDataOd.Value = dataOd.ToString();
            hDataDo.Value = dataDo.ToString();

            dOsobyJednostka.Items.Clear();
            dOsobyJednostka.Items.Add(new ListItem() { Text = Stale.wybierzOsobe, Value = Stale.wartoscZero });

            hJednostkaID.Value = Current.JednostkaID;
            //hYear.Value = lRok.Text;

            Model model = new Model();
            foreach (vKP_OsobyListaObecnosci item in model.GetListaObecnosci(int.Parse(Current.JednostkaID)))
            {
                dOsobyJednostka.Items.Add(new ListItem()
                {
                    Text = FunctionGlobal.UppercaseFirstWords(item.NazwiskoImie),
                    Value = item.OsobaID.ToString()
                });
            }
            dOsobyJednostka.SelectedIndex = 0;
            hOsobaID.Value = dOsobyJednostka.SelectedValue;

            //dCoIle.Items.Clear();
            //dCoIle.Items.Add(new ListItem() { Text = "2-gi", Value = "0" });
            //dCoIle.Items.Add(new ListItem() { Text = "3-ci", Value = "1" });
            //dCoIle.Items.Add(new ListItem() { Text = "4-ty", Value = "2" });
            //dCoIle.SelectedValue = "0";

            //dOdKtorego.Items.Clear();
            //dOdKtorego.Items.Add(new ListItem() { Text = "1-szego", Value = "0" });
            //dOdKtorego.Items.Add(new ListItem() { Text = "2-giego", Value = "1" });
            //dOdKtorego.Items.Add(new ListItem() { Text = "3-ciego", Value = "2" });
            //dOdKtorego.Items.Add(new ListItem() { Text = "4-tego", Value = "3" });
            //dOdKtorego.SelectedValue = "0";

            this.CalendarUC1.AllowHolidaysChecking = true;
            double dDni = (dataDo - dataOd).TotalDays;

            if (dDni < 35) CalendarUC1.NumberOfDisplayedMonths = 1;
            else
                if (dDni < 100) CalendarUC1.NumberOfDisplayedMonths = 3;
                else
                    if (dDni < 200) CalendarUC1.NumberOfDisplayedMonths = 6;
                    else
                        CalendarUC1.NumberOfDisplayedMonths = 12;



            this.CalendarUC1.DateStart = dataOd;

            //dCzasyPracy.Items.Clear();
            //dCzasyPracy.Items.Add(new ListItem() { Text = "propozycje czasu pracy", Value = "0" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 08:00 do 16:00", Value = "1" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 08:00 do 18:00", Value = "2" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 08:00 do 20:00", Value = "3" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 18:00 do 00:00", Value = "4" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 00:00 do 06:00", Value = "5" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 00:00 do 12:00", Value = "6" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 12:00 do 00:00", Value = "7" });
            //dCzasyPracy.Items.Add(new ListItem() { Text = "od 00:00 do 00:00", Value = "8" });
            //dCzasyPracy.SelectedValue = "0";

            List<DateTime> l = new List<DateTime>();
            foreach (var item in Kalendarz.GetSwieta(DateTime.Now.Date.Year))
            {
                l.Add(new DateTime(item.year, item.month, item.day));
            }
            if (hOsobaID.Value != Stale.wartoscZero)
                foreach (var item in Kalendarz.GetSwietaDodatkowe(DateTime.Now.Date.Year,
                        int.Parse(Current.FirmaID), Osoby.GetGrupaIDDlaOsobaID(int.Parse(hOsobaID.Value))))
                {
                    l.Add(new DateTime(item.year, item.month, item.day));
                }

            this.CalendarUC1.Holidays = l.ToArray();
            this.CalendarUC1.RenderCalendar();
        }
        private void zaznaczDniTygodnia(int dniaID, DateTime d1, DateTime d2)//0-niedz,1 poniedz....
        {

            List<DateTime> l = new List<DateTime>();
            for (DateTime d = d1; d <= d2; d = d.AddDays(1))
            {
                if ((int)d.DayOfWeek == dniaID && !CalendarUC1.Selected.Contains(d) && !Kalendarz.CzySwieto(d,
                        int.Parse(Current.FirmaID), Osoby.GetGrupaIDDlaOsobaID(int.Parse(hOsobaID.Value))))
                    l.Add(new DateTime(d.Year, d.Month, d.Day));
            }
            foreach (DateTime d in CalendarUC1.Selected) l.Add(new DateTime(d.Year, d.Month, d.Day));
            CalendarUC1.Selected = l.ToArray();
        }


        private void zaznaczCo(int coIle, int odDnia, bool czyRobocze)
        //coIle: 0 - co dwa, 1 co trzy ...
        //odDnia:0-pierwszego,1 drugiego,2 trzeciego,3-czwartego
        {
            CalendarUC1.ClearSelected();
            int i = -odDnia;
            List<DateTime> l = new List<DateTime>();
            for (DateTime d = dataOd; d <= dataDo; d = d.AddDays(1))
            {
                if (!czyRobocze || (int)d.DayOfWeek != 0 && (int)d.DayOfWeek != 6)
                {
                    if (i == 0)
                    {
                        if (!czyRobocze || !Kalendarz.CzySwieto(d,
                            int.Parse(Current.FirmaID), Osoby.GetGrupaIDDlaOsobaID(int.Parse(hOsobaID.Value))))
                        {
                            if (!CalendarUC1.Selected.Contains(d))
                                l.Add(new DateTime(d.Year, d.Month, d.Day));
                        }
                        i += 1;
                    }
                    else
                    {
                        if (i <= coIle) i += 1;
                        else i = 0;
                    }
                }
            }
            CalendarUC1.Selected = l.ToArray();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Error1.Visible = false;
            Error2.Visible = false;
            Error3.Visible = false;
            Error4.Visible = false;
            Error5.Visible = false;
            fError.Visible = false;
            if (!IsPostBack)
            {

                //////////////////

                //using (DBContainer db = new DBContainer())
                //{
                //    var t = (from wiz in db.vKP_KartyPracyDlaOsobJednostka.Where(a => a.JednostkaGlownaID == 194)  select wiz).ToArray();

                //}


                //////////////////

                dMonth.Items.Clear();
                dMonth.Items.Add(new ListItem() { Text = Stale.wybierzMiesiac, Value = Stale.wartoscZero });
                for (int i = 0; i < 12; i++)
                {
                    dMonth.Items.Add(new ListItem()
                    {
                        Text = MonthNames[i],
                        Value = (i + 1).ToString()
                    });
                }
                dMonth.SelectedIndex = 0;
                dKwartal.Items.Clear();
                dKwartal.Items.Add(new ListItem() { Text = Stale.wybierzKwartal, Value = Stale.wartoscZero });
                for (int j = 0; j < 4; j++)
                {
                    dKwartal.Items.Add(new ListItem()
                    {
                        Text = KwartalNames[j],
                        Value = (j + 1).ToString()
                    });
                }
                dKwartal.SelectedIndex = 0;
                dYear.Items.Clear();
                foreach (short item in Kalendarz.GetYearGdzieWpisanoSwieta())
                {
                    this.dYear.Items.Add(item.ToString());
                }
                dYear.SelectedValue = DateTime.Now.Year.ToString();
                lRok.Text = dYear.SelectedValue;
                hJednostkaID.Value = Current.JednostkaID;
                //this.ListView2.DataBind();
                dCzasyPracy.Items.Clear();
                dCzasyPracy.Items.Add(new ListItem() { Text = "propozycje czasu pracy", Value = "0" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 08:00 do 16:00", Value = "1" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 08:00 do 18:00", Value = "2" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 08:00 do 20:00", Value = "3" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 18:00 do 00:00", Value = "4" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 00:00 do 06:00", Value = "5" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 00:00 do 12:00", Value = "6" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 12:00 do 00:00", Value = "7" });
                dCzasyPracy.Items.Add(new ListItem() { Text = "od 00:00 do 00:00", Value = "8" });
                dCzasyPracy.SelectedValue = "0";

                dCzasPrzerwy.Items.Clear();
                dCzasPrzerwy.Items.Add(new ListItem() { Text = "brak przerwy w pracy", Value = "0" });
                dCzasPrzerwy.Items.Add(new ListItem() { Text = "przerwa od 12:00 do 13:00", Value = "1" });
                dCzasPrzerwy.Items.Add(new ListItem() { Text = "przerwa od 13:00 do 14:00", Value = "2" });
                dCzasPrzerwy.Items.Add(new ListItem() { Text = "przerwa od 14:00 do 15:00", Value = "3" });
                dCzasPrzerwy.SelectedValue = "0";

                dInnyOkres.Items.Clear();
                dInnyOkres.Items.Add(new ListItem() { Text = Stale.wybierzInnyOkres, Value = Stale.wartoscZero });
                dInnyOkres.Items.Add(new ListItem() { Text = Stale.IPolrocze, Value = "1" });
                dInnyOkres.Items.Add(new ListItem() { Text = Stale.IIPolrocze, Value = "2" });
                dInnyOkres.Items.Add(new ListItem() { Text = Stale.calyRok, Value = "3" });
                dInnyOkres.SelectedIndex = 0;
              
                dCoIle.Items.Clear();
                dCoIle.Items.Add(new ListItem() { Text = "2-gi", Value = "0" });
                dCoIle.Items.Add(new ListItem() { Text = "3-ci", Value = "1" });
                dCoIle.Items.Add(new ListItem() { Text = "4-ty", Value = "2" });
                dCoIle.SelectedValue = "0";

                dOdKtorego.Items.Clear();
                dOdKtorego.Items.Add(new ListItem() { Text = "1-szego", Value = "0" });
                dOdKtorego.Items.Add(new ListItem() { Text = "2-giego", Value = "1" });
                dOdKtorego.Items.Add(new ListItem() { Text = "3-ciego", Value = "2" });
                dOdKtorego.Items.Add(new ListItem() { Text = "4-tego", Value = "3" });
                dOdKtorego.SelectedValue = "0";
            }
            else
            {
                this.CalendarUC1.RenderCalendar();
            }
        }


        protected int KartaPracyPozycjaUtworz(int year, int month, int day)
        // 0 OK, -1 bład zapisu, -2 OD>=DO z wyjątkiem od 0:0 do 0:0
        {
            DateTime czasOd = new DateTime(year, month, day, tOd.Hour, tOd.Minute, 00);
            DateTime czasDo = new DateTime(year, month, day, tDo.Hour, tDo.Minute, 00);
            DateTime dataKarta = new DateTime(year, month, day);
            double czas = FunctionGlobal.TimeDifferenceInMinutes(czasDo, czasOd);

            if (czas < 0 || czas == 0 && (dataOd.Hour != 0 || dataOd.Minute != 0))
            {
                Error2.Visible = true;
                return -2;
            }

            OsobyKartaPracy os = new OsobyKartaPracy();
            int iT = os.InsertUpdateKartaPracy(int.Parse(hOsobaID.Value), czasOd, czasDo, dataKarta, (int)Math.Round(czas), 0);

            if (iT == -1)
            {
                Error1.Visible = true;
                return -1;
            }
            return 0;
        }


        protected void ZapiszKartePracy_Click(object sender, System.EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }

            bool btemp = false;
            foreach (DateTime d in this.CalendarUC1.Selected)
            {
                if (KartaPracyPozycjaUtworz(d.Year, d.Month, d.Day) != 0) btemp = true;
            }
            if (btemp) komunikatError(-1);
            this.ListView1.DataBind();
        }

        protected void KasujZaznaczoneDniZKartyPracy_Click(object sender, System.EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            bool btemp = false;
            foreach (DateTime d in this.CalendarUC1.Selected)
            {
                if (OsobyKartaPracy.DeleteKartaPracyDzien(int.Parse(hOsobaID.Value), new DateTime(d.Year, d.Month, d.Day)) != 0) btemp = true;
            }
            if (btemp) komunikatError(-1);
            this.ListView1.DataBind();
        }

        protected void KasujKartePracy_Click(object sender, System.EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            if (OsobyKartaPracy.DeleteKartaPracy(int.Parse(hOsobaID.Value), dataOd, dataDo) != 0) komunikatError(-1);
            else this.ListView1.DataBind();
        }


        protected void CalendarUC_UserControlRefreshedE(object sender, CalendarUC.ActionEvent e)
        {
            //tutaj odswiez u siebie dni
            if (sender == null) return;
            int iDni = (sender as CalendarUC).Selected.Count();
            //tZaznaczone.Text = iDni.ToString();
            //tRobocze.Text = Kalendarz.GetLiczbaDniRoboczych((sender as CalendarUC).Selected).ToString();

            if (e.ActionType == CalendarUC.ActionType.NextPrevMonth || e.ActionType == CalendarUC.ActionType.PageLoad)
            {
                this.CalendarUC1.ClearUnavailablePeriods();
                DateTime since = new DateTime((sender as CalendarUC).DateStart.Year, (sender as CalendarUC).DateStart.Month, 1);
                DateTime until = new DateTime(since.Year, since.Month, DateTime.DaysInMonth(since.Year, since.Month));

                DateTime zatrOd = DateTime.Parse(Current.DataZatrudnieniaOd);
                DateTime zatrDo = DateTime.Parse(Current.DataZatrudnieniaDo);

                if (zatrOd > since && zatrOd > until || zatrDo < since && zatrDo < until) (sender as CalendarUC).AddUnavailablePeriod(since, until);
                else
                    if (zatrOd > since && zatrOd < until) (sender as CalendarUC).AddUnavailablePeriod(since, zatrOd.AddDays(-1));
                if (zatrDo > since && zatrDo < until) (sender as CalendarUC).AddUnavailablePeriod(zatrDo.AddDays(1), until);
            }
        }

        protected void dCzasyPracy_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (dCzasyPracy.SelectedIndex)
            {
                case 0:
                    tOd.Hour = 0;
                    tOd.Minute = 0;
                    tDo.Hour = 0;
                    tDo.Minute = 0;
                    break;
                case 1:
                    tOd.Hour = 8;
                    tOd.Minute = 0;
                    tDo.Hour = 16;
                    tDo.Minute = 0;
                    break;
                case 2:
                    tOd.Hour = 8;
                    tOd.Minute = 0;
                    tDo.Hour = 18;
                    tDo.Minute = 0;
                    break;
                case 3:
                    tOd.Hour = 8;
                    tOd.Minute = 0;
                    tDo.Hour = 20;
                    tDo.Minute = 0;
                    break;
                case 4:
                    tOd.Hour = 18;
                    tOd.Minute = 0;
                    tDo.Hour = 0;
                    tDo.Minute = 0;
                    break;
                case 5:
                    tOd.Hour = 0;
                    tOd.Minute = 0;
                    tDo.Hour = 6;
                    tDo.Minute = 0;
                    break;
                case 6:
                    tOd.Hour = 0;
                    tOd.Minute = 0;
                    tDo.Hour = 12;
                    tDo.Minute = 0;
                    break;
                case 7:
                    tOd.Hour = 12;
                    tOd.Minute = 0;
                    tDo.Hour = 0;
                    tDo.Minute = 0;
                    break;
                case 8:
                    tOd.Hour = 0;
                    tOd.Minute = 0;
                    tDo.Hour = 0;
                    tDo.Minute = 0;
                    break;
            }
        }

        protected void Wszystkie_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            CalendarUC1.ClearSelected();

            List<DateTime> l = new List<DateTime>();
            for (DateTime d = dataOd; d <= dataDo; d = d.AddDays(1))
            {
                if (!CalendarUC1.Selected.Contains(d))
                    l.Add(new DateTime(d.Year, d.Month, d.Day));
            }
            CalendarUC1.Selected = l.ToArray();
        }
        protected void Robocze_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            CalendarUC1.ClearSelected();

            List<DateTime> l = new List<DateTime>();
            for (DateTime d = dataOd; d <= dataDo; d = d.AddDays(1))
            {
                if ((int)d.DayOfWeek != 0 && (int)d.DayOfWeek != 6 && !CalendarUC1.Selected.Contains(d) &&
                    !Kalendarz.CzySwieto(d, int.Parse(Current.FirmaID), Osoby.GetGrupaIDDlaOsobaID(int.Parse(hOsobaID.Value))))
                    l.Add(new DateTime(d.Year, d.Month, d.Day));
            }
            CalendarUC1.Selected = l.ToArray();
        }
        protected void btnViewClick(object sender, EventArgs e)
        {
            //int outProc = UrlopyK.DeleteWniosekUrlopowy(
            //               int.Parse(((Button)sender).CommandArgument.ToString()));
            //if (outProc != 0) Error2.Visible = true;
            //this.ListView1.DataBind();

            fListaEdycja.Visible = true;
        
        
        
        }
        protected void btnDrukujClick(object sender, EventArgs e)
        {
            string sTemp = "123456";

            for (int i = sTemp.Length; i < Stale.countDigitsInBarCode - 2; i++)
            {
                sTemp = "0" + sTemp;
            }
            sTemp = Stale.idPU + sTemp;

            //int.Parse(((Button)sender).CommandArgument.ToString())
            //string [][] parametry= new string[2][];
            //parametry[0]= new 

            RaportyPDF.RaportHarmonogramBarCodePDF(int.Parse(sTemp), int.Parse(hJednostkaID.Value), int.Parse(((Button)sender).CommandArgument.ToString()), 3,int.Parse(dYear.SelectedValue));

        }
        protected void Wyczysc_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            CalendarUC1.ClearSelected();
            this.CalendarUC1.RenderCalendar();
        }


        protected void CoIleR_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            switch (dCoIle.SelectedValue)
            {
                case "0": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), true);
                    break;
                case "1": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), true);
                    break;
                case "2": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), true);
                    break;
                case "3": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), true);
                    break;
            }
        }

        protected void CoIleW_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            switch (dCoIle.SelectedValue)
            {
                case "0": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), false);
                    break;
                case "1": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), false);
                    break;
                case "2": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), false);
                    break;
                case "3": zaznaczCo(int.Parse(dCoIle.SelectedValue), int.Parse(dOdKtorego.SelectedValue), false);
                    break;
            }
        }
        protected void Pon_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            zaznaczDniTygodnia(1, dataOd, dataDo);
        }
        protected void Wto_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            zaznaczDniTygodnia(2, dataOd, dataDo);
        }
        protected void Sro_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            zaznaczDniTygodnia(3, dataOd, dataDo);
        }
        protected void Czw_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            zaznaczDniTygodnia(4, dataOd, dataDo);
        }
        protected void Pia_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            zaznaczDniTygodnia(5, dataOd, dataDo);
        }
        protected void Sob_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            zaznaczDniTygodnia(6, dataOd, dataDo);
        }
        protected void Nie_Click(object sender, EventArgs e)
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            zaznaczDniTygodnia(0, dataOd, dataDo);
        }
        protected void tOd_Load(object sender, EventArgs e)
        {
            tOd.UstawCzas(8, 0);
            tDo.UstawCzas(16, 0);
        }

        protected void dOsobyJednostka_SelectedIndexChanged(object sender, EventArgs e)
        {
            int iTemp = 0;
            hOsobaID.Value = dOsobyJednostka.SelectedValue;
            if (hOsobaID.Value != Stale.wartoscZero && cPoczatkoweUstawienia.Checked)
            {
                List<DateTime> l = new List<DateTime>();
                foreach (var item in Kalendarz.GetSwieta(DateTime.Now.Date.Year))
                {
                    l.Add(new DateTime(item.year, item.month, item.day));
                }

                foreach (var item in Kalendarz.GetSwietaDodatkowe(DateTime.Now.Date.Year,
                        int.Parse(Current.FirmaID), Osoby.GetGrupaIDDlaOsobaID(int.Parse(hOsobaID.Value))))
                {
                    l.Add(new DateTime(item.year, item.month, item.day));
                }

                this.CalendarUC1.Holidays = l.ToArray();
                CalendarUC1.ClearSelected();

                l.Clear();
                foreach (vKP_KartyPracy item in OsobyKartaPracy.GetKartaPracyDlaOsoby(int.Parse(hOsobaID.Value), dataOd, dataDo))
                {
                    l.Add((DateTime)item.DzienKartyPracy);
                    iTemp += 1;
                }
                this.CalendarUC1.Selected = l.ToArray();

                CalendarUC1.RenderCalendar();
            }
            this.ListView1.DataBind();
        }

   
        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            string czasWGodz = FunctionGlobal.ZamienNaGodziny(int.Parse(((Label)e.Item.FindControl("lCzasWMinutach")).Text));
            ((Label)e.Item.FindControl("lCzasWGodz")).Text = czasWGodz;
        }
     
        
        protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ((Label)e.Item.FindControl("lCzasPracyMiesiac")).Text = FunctionGlobal.ZamienNaGodziny(int.Parse(((Label)e.Item.FindControl("lCzasPracyMiesiac")).Text));
            ((Label)e.Item.FindControl("lNazwiskoImie")).Text = FunctionGlobal.UppercaseFirstWords(((Label)e.Item.FindControl("lNazwiskoImie")).Text);
            ((Label)e.Item.FindControl("lMiesiacKartyPracy")).Text = MonthNames[int.Parse(((Label)e.Item.FindControl("lMiesiacKartyPracy")).Text) - 1] + " " + lRok.Text;
        }
     
        
        protected void dMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dMonth.SelectedIndex > 0)
            {
                fMiesiacKwartal.Visible = false;
                fParametry.Visible = true;
                fListaEdycja.Visible = true;
                inicKartaPracy(dMonth.SelectedIndex);
            }
        }
        protected void dYear_SelectedIndexChanged(object sender, EventArgs e)
        {
                lRok.Text = dYear.SelectedValue;
                hJednostkaID.Value = Current.JednostkaID;
                this.ListView2.DataBind();
        }
        protected void dKwartal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dKwartal.SelectedIndex > 0)
            {
                fMiesiacKwartal.Visible = false;
                fParametry.Visible = true;
                fListaEdycja.Visible = true;
                inicKartaPracy(101 + (dKwartal.SelectedIndex - 1) * 3);
            }
        }
        protected void dInnyOkres_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dInnyOkres.SelectedIndex > 0)
            {
                fMiesiacKwartal.Visible = false;
                fParametry.Visible = true;
                fListaEdycja.Visible = true;
                inicKartaPracy(200 + dInnyOkres.SelectedIndex);
            }
        }
        
        protected void PrzeslijDoKadr_Click(object sender, EventArgs e)
        // -1 bład zapisu, -3 nie wybrano osoby, -5 Harmonogram w miesiącu musi mieć co najmniej jedna pozycję
        {
            if (hOsobaID.Value == Stale.wartoscZero)
            {
                komunikatError(-3);
                return;
            }
            bool bTemp = false;
            for (int iM = dataOd.Month; iM <=dataDo.Month; iM++)
            {
                if (OsobyKartaPracy.GetKartaPracyDlaOsoby(int.Parse(hOsobaID.Value), dataOd, dataDo).Count() == 0)
                {
                    bTemp = true;
                    break;
                }
            }
            if (bTemp)
            {
                komunikatError(-5);
                return;
            }
            int outProc = OsobyKartaPracy.UpdateStanKartyPracy(int.Parse(hOsobaID.Value), dataOd, dataDo, 0, Stale.stanDoKadr);
            if (outProc==-1)
            {
                komunikatError(-1);
                return;
            }
            this.ListView1.DataBind();
        }
    }
}